package com.vitabuybuy.tv.ui.mine.coin

import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.library.common.base.BaseActivity
import com.library.common.base.BindRecycAdapter
import com.library.common.base.MessageModel
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityRechargeRewardCoinBinding
import com.vitabuybuy.tv.databinding.ItemCoinBinding
import com.vitabuybuy.tv.dialog.RechargePopup
import com.vitabuybuy.tv.dto.RechargeDto
import com.library.common.toast.XToast
import com.library.common.widget.RecycleViewDivider
import com.vitabuybuy.tv.ui.mine.coin.viewmodel.RewardCoinViewModel
import kotlinx.android.synthetic.main.activity_recharge_reward_coin.*

class RechargeRewardCoinActivity :
    BaseActivity<RewardCoinViewModel, ActivityRechargeRewardCoinBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_recharge_reward_coin

    private val coinAdapter by lazy { CoinAdapter() }

    private var coinNum: Int? = 0

    override fun onGetBundle(bundle: Bundle?) {
        coinNum = bundle?.getInt("coinNum")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.recharge_reward_coin))
        tvBalance.text = coinNum.toString()
        listview.apply {
            adapter = coinAdapter
            layoutManager = LinearLayoutManager(mContext)
            addItemDecoration(RecycleViewDivider(mContext, 1))
        }
    }

    override fun observeData() {
        viewModel.getRechargeList()
        viewModel.rechargelist.observe(this, Observer {
            coinAdapter.setNewData(it)
        })
    }

    override fun eventHandling() {
        coinAdapter.setOnItemClickListener { _, item, _ ->
            RechargePopup(mContext) {
                viewModel.rechargeCoin(item.id, item.goldQuantity, it)
            }.showPopupWindow()
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            1 -> {
                XToast.success(getString(R.string.recharge_success))
                val goldQuantity = msg.arg1
                tvBalance.text = ((tvBalance.text.toString().toInt()) + goldQuantity).toString()
            }
        }
    }

    class CoinAdapter : BindRecycAdapter<ItemCoinBinding, RechargeDto>() {
        override fun onBindViewHolder(
            mBinding: ItemCoinBinding,
            itemData: RechargeDto,
            holder: RecyclerView.ViewHolder,
            position: Int
        ) {
            mBinding.tvAmount1.text = itemData.goldQuantity.toString()
            mBinding.btnPay1.text = String.format(
                mContext.getString(R.string.price_with_value),
                itemData.rechargeAmount
            )
        }

        override fun normalViewId(viewType: Int): Int {
            return R.layout.item_coin
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, coinNum: Int) {
            mContext.startActivity(
                bundleOf(Pair("coinNum", coinNum)),
                RechargeRewardCoinActivity::class.java
            )
        }
    }
}
